import { type Ref, ref, watch } from 'vue'
export default class VarUtils {
  public static await<T, E>(
    t: Ref<T | undefined>,
    check: (t: Ref<T | undefined>) => E | undefined,
    onHasValue?: (e: E) => void
  ): Ref<E | undefined> {
    const valueRef = ref<E>()
    watch(
      () => check(t),
      (newVal) => {
        if (newVal != null) {
          valueRef.value = newVal
          if (onHasValue != null) onHasValue(newVal)
        }
      }
    )
    return valueRef
  }
}
